package com.meteor.user.controller;

import com.meteor.framework.common.base.ResponseData;
import com.meteor.framework.common.utils.PageResult;
import com.meteor.framework.operatelog.annotations.OperateLog;
import com.meteor.framework.operatelog.enums.OperateTypeEnum;
import com.meteor.user.convert.SysDepartmentConvert;
import com.meteor.user.convert.SysDictDataConvert;
import com.meteor.user.convert.SysPostConvert;
import com.meteor.user.entity.SysDepartment;
import com.meteor.user.entity.SysPost;
import com.meteor.user.query.SysDepartmentQuery;
import com.meteor.user.query.SysPostAndOrgQuery;
import com.meteor.user.query.SysPostQuery;
import com.meteor.user.service.SysDepartmentService;
import com.meteor.user.service.SysPostService;
import com.meteor.user.vo.SysDepartmentVO;
import com.meteor.user.vo.SysPostVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 部门管理
 *
 * @author lynn
 * @date 2024/1/03
 */
@RestController
@RequestMapping("sys/department")
@Tag(name = "部门管理")
@AllArgsConstructor
public class SysDepartmentController {

    private final SysDepartmentService sysDepartmentService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('sys:department:page')")
    public ResponseData page(@ParameterObject @Valid SysDepartmentQuery query) {
        PageResult<SysDepartmentVO> page = sysDepartmentService.page(query);

        return ResponseData.success(page);
    }

    @GetMapping("list")
    @Operation(summary = "列表")
    public ResponseData list() {
        List<SysDepartmentVO> list = sysDepartmentService.getList();

        return ResponseData.success(list);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('sys:post:info')")
    public ResponseData get(@PathVariable("id") String id) {

        SysDepartment entity = sysDepartmentService.getById(id);

        return ResponseData.success(SysDepartmentConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(type = OperateTypeEnum.INSERT)
    @PreAuthorize("hasAuthority('sys:department:save')")
    public ResponseData save(@RequestBody SysDepartmentVO vo) {
        sysDepartmentService.save(vo);

        return ResponseData.success("新增成功");
    }

    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('sys:department:update')")
    public ResponseData update(@RequestBody @Valid SysDepartmentVO vo) {
        sysDepartmentService.update(vo);

        return ResponseData.success("修改成功");
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('sys:department:delete')")
    public ResponseData delete(@RequestBody List<String> idList) {
        sysDepartmentService.delete(idList);

        return ResponseData.success("删除成功");
    }

    @PostMapping("/departmentMerchantOrg")
    @Operation(summary = "根据商户id和组织id查询岗位")
    @PreAuthorize("hasAuthority('sys:department:all')")
    public ResponseData departmentMerchantOrg(@RequestBody @Valid SysPostAndOrgQuery query) {
        List<SysDepartmentVO> list = sysDepartmentService.departmentMerchantOrg(query);
        return ResponseData.success("查询成功",list);
    }
}